package com.house.controller;

import cn.hutool.core.bean.BeanUtil;
import com.house.dto.BillDTO;
import com.house.entity.Bill;
import com.house.service.BillService;
import com.house.service.HouseService;
import com.house.utils.Code;
import com.house.utils.Result;
import com.house.vo.BillVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.*;

/**
 * @author: GGP
 * @description: 订单控制器
 * @time: 2023-04-16 11:48
 **/
@Api("订单")
@RestController
@RequestMapping("/bill")
@Validated
public class BillController {

	@Resource
	private BillService billService;

	@Resource
	private HouseService houseService;

	@ApiOperation(value = "添加订单")
	@PostMapping("/save")
	public Result saveBill(@RequestBody @Valid BillDTO billDTO){
		billService.saveBill(billDTO);
		return new Result(Code.SAVE_OK,"添加成功");
	}

	@ApiOperation(value = "查询订单",notes = "多条件查询订单")
	@PostMapping("/find")
	public Result findBills(@RequestBody BillDTO billDTO){
		Page<Map<String, Object>> bills =billDTO.getRoomNum()!=null
				?billService.findByRoomNum(billDTO)
				:billService.findBills(billDTO);
		Map<String,Object> map=new HashMap<>(3);
		List<Map<String, Object>> content = bills.getContent();
		List<BillVO> billsVO = BeanUtil.copyToList(content, BillVO.class);
		billsVO.forEach(billVO-> {
					Integer status = billVO.getStatus();
					if (status == 1) {
						billVO.setStatusMsg("已预订");
						billVO.setButtonMsg("入住");
					} else if (status == 2) {
						billVO.setStatusMsg("已入住");
						billVO.setButtonMsg("退房");
					} else if (status == 3) {
						billVO.setStatusMsg("已退房");
						billVO.setButtonMsg("已完成");
					} else {
						billVO.setStatusMsg("已完成");
						billVO.setButtonMsg("");
					}
				});
		map.put("bills",billsVO);
		map.put("totalPages",bills.getTotalPages());
		map.put("totalElements",bills.getTotalElements());
		return new Result(Code.GET_OK,map,"查询成功");
	}

	/**
	 * 订单号查询
	 * @param billNumber
	 * @return
	 */
	@ApiOperation(value = "订单号查询",notes = "")
	@GetMapping("/findOne/{billNumber}")
	public Result findOne(@PathVariable Integer billNumber){
		BillVO billVO = billService.findOne(billNumber);
		if (billVO==null){
			return new Result(Code.GET_ERR,"未查询到订单");
		}
		Integer status = billVO.getStatus();
		if (status == 1) {
			billVO.setStatusMsg("已预订");
			billVO.setButtonMsg("入住");
		} else if (status == 2) {
			billVO.setStatusMsg("已入住");
			billVO.setButtonMsg("退房");
		} else if (status == 3) {
			billVO.setStatusMsg("已退房");
			billVO.setButtonMsg("已完成");
		} else {
			billVO.setStatusMsg("已完成");
			billVO.setButtonMsg("");
		}
		List<BillVO> list=new ArrayList<>();
		Map<String,Object> map=new HashMap<>(3);
		list.add(billVO);
		map.put("bills",list);
		map.put("totalPages",1);
		map.put("totalElements",1);
		return new Result(Code.GET_OK,map,"查询成功");
	}

	/**
	 * 用户id查询-获取用户订单
	 * @param billDTO
	 * @return
	 */
	@ApiOperation(value = "获取用户订单",notes = "")
	@PostMapping("/findByUserId")
	public Result findByUserId(@RequestBody BillDTO billDTO){
		Map<String,Object> map=new HashMap<>(3);
		Page<Map<String, Object>> byUserId = billService.findByUserId(billDTO);
		List<Map<String, Object>> content = byUserId.getContent();
		List<BillVO> billsVO = BeanUtil.copyToList(content, BillVO.class);
		map.put("bills",billsVO);
		map.put("totalPages",byUserId.getTotalPages());
		map.put("totalElements",byUserId.getTotalElements());
		return new Result(Code.GET_OK,map,"查询成功");
	}

	/**
	 * 修改订单状态
	 * @param billDTO
	 * @return
	 */
	@ApiOperation(value = "修改订单状态")
	@PostMapping("/update")
	public Result update (@RequestBody BillDTO billDTO){
		Optional<Bill> byId = billService.findById(billDTO.getId());
		if (!byId.isPresent()) {
			return new Result(Code.SYSTEM_ERR,"系统错误，请联系管理员！");
		}
		Bill bill = byId.get();
		billService.update(billDTO);
		houseService.updateScore(bill.getHouseId());
		return new Result(Code.UPDATE_OK,"操作成功");
	}

	/**
	 * 显示财务状况
	 * @return
	 */
	@ApiOperation(value = "计算近一个月收入和总收入")
	@GetMapping("/totalAmount/{publisherId}")
	public Result totalAmount(@PathVariable Integer publisherId){
		Map<String,String> map =new HashMap<>(4);
		String amount = billService.grossAmount(publisherId);
		String amounts = billService.grossAmounts(publisherId);
		map.put("amount",amount);
		map.put("amounts",amounts);
		return new Result(Code.GET_OK,map,"计算成功");
	}
}
